/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easemob.chatui.domain;

import com.easemob.chat.EMContact;
import com.easemob.chatui.Constant;
import com.easemob.util.HanziToPinyin;
import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.enums.AssignType;
import com.lyw.GeoLocationClient.utils.StringUtils;

public class User extends EMContact {
    @PrimaryKey(AssignType.AUTO_INCREMENT)
    @Column("__id")
    protected long id;

    public User(String username, String nick, String polId, String unit, String group, String imei) {
        this.username = username;
        this.nick = nick;
        this.pol_id = polId;
        this.unit = unit;
        this._group = group;
        this.imei = imei;
        setUserHearder();
    }

    @Override
    public void setNick(String s) {
        super.setNick(s);
        setUserHearder();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

	private int unreadMsgCount;
	private String header;
	private String avatar;
    private int geo_id = -1;
    private String pol_id;
    private String unit;
    private String _group;
    private String imei;
    private String time;
    private String password;

    public User(){}
	
	public User(String username){
	    this.username = username;
	}

	public String getHeader() {
		return header;
	}

	public void setHeader(String header) {
		this.header = header;
	}

	public int getUnreadMsgCount() {
		return unreadMsgCount;
	}

	public void setUnreadMsgCount(int unreadMsgCount) {
		this.unreadMsgCount = unreadMsgCount;
	}
	
	

	public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getGeo_id() {
        return geo_id;
    }

    public void setGeo_id(int geo_id) {
        this.geo_id = geo_id;
    }

    public String getPol_id() {
        return pol_id;
    }

    public void setPol_id(String pol_id) {
        this.pol_id = pol_id;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String get_group() {
        return _group;
    }

    public void set_group(String _group) {
        this._group = _group;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
	public int hashCode() {
		return 17 * getUsername().hashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof User)) {
			return false;
		}
		return getUsername().equals(((User) o).getUsername());
	}

    private void setUserHearder() {
        String headerName = getDisplayName();
        if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
            setHeader("");
        } else if (Character.isDigit(headerName.charAt(0))) {
            setHeader("#");
        } else {
            setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1)).get(0).target.substring(0, 1).toUpperCase());
//            char header = headerName.toLowerCase().charAt(0);
//            if (header < 'a' || header > 'z') {
//                setHeader("#");
//            }
        }
    }

    public String getDisplayName() {
        if (!StringUtils.isNullOrEmpty(nick)) {
            return nick;
        } else {
            return username;
        }
    }

	@Override
	public String toString() {
		return nick == null ? username : nick;
	}
}
